﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using YXShop.BLL.Admin;

namespace YXShop.Web.admin.member
{
    public partial class member_bank_remit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitWebControl();
                MemberBind();
                BankBind();
            }
        }

        #region 验证
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtCoupons, "输入数字将作为点卷金额，默认为0", "isint", "必填", "该项必须为数字类型");
            ChangeHope.WebPage.WebControl.Validate(this.txtMoney, "输入数字将作为汇款的金额", "isint", "必填", "该项必须为数字类型");
            ChangeHope.WebPage.WebControl.SetDate(this.txtEndDate);
            this.txtEndDate.Text = DateTime.Now.ToShortDateString();
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion

        #region 信息绑定
        protected void MemberBind()
        {
            int uid = ChangeHope.WebPage.PageRequest.GetQueryInt("uid");
            string outDate = "0";
            YXShop.BLL.Member.MemberAccount bll = new YXShop.BLL.Member.MemberAccount();
            YXShop.Model.Member.MemberAccount model = bll.GetModel(uid);
            lblUserId.Text = model.UserId;
            lblGroupName.Text = GetGroupName(Convert.ToInt32(model.UserGroup));
            lblCapital.Text = model.Capital.ToString();
            lblCoupons.Text = model.Coupons.ToString();
            lblPoints.Text = model.Points.ToString();
            if (Convert.ToDateTime(model.PeriodOfValidity) > System.DateTime.Now)
            {
                TimeSpan ts1 = new TimeSpan(Convert.ToDateTime(model.PeriodOfValidity).Ticks);
                TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                outDate = ts.Days.ToString();
            }
            lblPeriodOfValidity.Text = outDate;
        }

        protected string GetGroupName(int groupId)
        {
            YXShop.BLL.Member.MemberRank bll = new YXShop.BLL.Member.MemberRank();
            YXShop.Model.Member.MemberRank model = bll.GetModel(groupId);
            if (model != null)
            {
                return model.Name;
            }
            else
            {
                return "无该用户组";
            }
        }

        protected void BankBind()
        {
            YXShop.BLL.SystemInfo.BankInfo bll = new YXShop.BLL.SystemInfo.BankInfo();
            List<YXShop.Model.SystemInfo.BankInfo> model = bll.GetAll();
            ddlBankType.DataSource = model;
            ddlBankType.DataTextField = "BankName";
            ddlBankType.DataValueField = "ID";
            ddlBankType.DataBind();
        }
        #endregion

        protected string GetUserID(int uid)
        {
            YXShop.BLL.Member.MemberAccount bll = new YXShop.BLL.Member.MemberAccount();
            YXShop.Model.Member.MemberAccount model = bll.GetModel(uid);
            if (model != null)
            {
                return model.UserId;
            }
            else
            {
                return "无该用户";
            }
        }

        protected void lbSave_Click(object sender, EventArgs e)
        {
            int uid = ChangeHope.WebPage.PageRequest.GetQueryInt("uid");
            YXShop.Model.Admin.AdminInfo adminModel = (YXShop.Model.Admin.AdminInfo)YXShop.Common.AdministrorManager.Get();           
            YXShop.Model.Member.UserinAndExp model = new YXShop.Model.Member.UserinAndExp();
            YXShop.BLL.Member.UserinAndExp bll = new YXShop.BLL.Member.UserinAndExp();
            YXShop.BLL.Member.MemberAccount abll = new YXShop.BLL.Member.MemberAccount();
            model.UID = uid;
            try
            {
                model.AdsumMoneyDate = Convert.ToDateTime(this.txtEndDate.Text);
            }
            catch
            {
                this.ltlMsg.Text = "请输入正确的时间格式，最好通过选择器选择日期";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            model.AdsumMoney = Convert.ToDecimal(this.txtMoney.Text.Trim());
            //更新MemberAccount的资金数量
            abll.Amend(uid, "Capital", Convert.ToDecimal(this.lblCapital.Text) + Convert.ToDecimal(this.txtMoney.Text));
            model.RemitMode = 1;
            model.RemitBank = this.ddlBankType.SelectedItem.Text;
            model.Remark = this.txtRemark.Text.Trim();
            model.FormMode = "";
            model.BosomNote = this.txtRcord.Text.Trim();
            model.NoteDate = DateTime.Now;
            model.NoteName = adminModel.AdminName;
            //系统给用户汇款记录 算作用户的收入记录
            model.InComeandExpState = 0;
            model.State = Convert.ToInt32(this.ddlState.SelectedValue);
            //对应bankaccount表中的bank_account 表示会员登录账号
            model.UserId = GetUserID(uid);
            if (cbxSendCoupons.Checked)
            {
                //更新用户表点卷数量
                abll.Amend(uid, "Coupons", Convert.ToDecimal(this.lblCoupons.Text) + Convert.ToDecimal(this.txtCoupons.Text));
                //添加点卷明细记录
                YXShop.Model.Member.UserInfoNote noteModel = new YXShop.Model.Member.UserInfoNote();
                YXShop.BLL.Member.UserInfoNote noteBll = new YXShop.BLL.Member.UserInfoNote();
                noteModel.UserID = uid;
                noteModel.TicketCount = Convert.ToDecimal(this.txtCoupons.Text);
                noteModel.Causation = this.txtRemark.Text;
                noteModel.BosomNote = this.txtRcord.Text;
                noteModel.NoteDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
                noteModel.NoteName = adminModel.AdminName;
                noteModel.NoteType = 0;
                noteModel.BuckleOrAdd = 0;
                noteModel.Username = GetUserID(uid);
                noteBll.Add(noteModel);

            }
            //赠送的点卷
            model.PresentCoupons = cbxSendCoupons.Checked ? Convert.ToDecimal(txtCoupons.Text.Trim()) : 0;
            bll.Add(model);
            this.ltlMsg.Text = "操作成功！";
            this.pnlMsg.Visible = true;
            this.pnlMsg.CssClass = "actionOk";
        }
    }
}
